// Package avatar -----------------------------
// @file      : avatarRoomPmtModel.go
// @author    : WuZhouHai
// @contact   : wzh200x@126.com
// @time      : 2024/7/24 17:25
// @desc      :
// -------------------------------------------
package avatar

import (
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gmeta"
	"phoenix/apps/examples/phoenix_app_dwys/defines"
	"phoenix/apps/examples/phoenix_app_dwys/model/gameConfig"
	"phoenix/phoenix/common/logger"
	"phoenix/phoenix/defines/dataType"
	"phoenix/phoenix/entitys/entityModel"
	"phoenix/phoenix/interfaces"
	"phoenix/phoenix/managers/databaseManager"
	"phoenix/phoenix/managers/logicConfigManager"
	"phoenix/phoenix/utils"
)

type AvatarRoomPmtModelData struct {
	gmeta.Meta `orm:"table: module_room_pmt"`
	Dbid       dataType.PhoenixTypeDataBaseId `orm:"id"`
	AvatarId   dataType.PhoenixTypeDataBaseId `orm:"ownerId"`
	UpdateTime string                         `orm:"update_time"`
	RoomId     int32                          `orm:"roomId"` // 店铺类型
	PmtId      int32                          `orm:"pmtId"`
	Level      int32                          `orm:"level"`
	PmtLevel   int32                          `orm:"pmtLevel"`
	Name       string                         `orm:"name"`
}

func NewAvatarRoomOmtModelData() *AvatarRoomPmtModelData {
	return &AvatarRoomPmtModelData{}
}

type AvatarRoomPmtModel struct {
	entityModel.ModuleModel
}

func (this *AvatarRoomPmtModel) LoadModule() error {
	var records []AvatarRoomPmtModelData
	err := this.LoadModuleFromDB(&records)
	if err != nil {
		return err
	}
	// 遍历记录
	if len(records) == 0 {
		err := this.Create()
		if err != nil {
			return err
		}
	} else {
		for i, record := range records {
			this.SetModelData(record.Dbid, &records[i])
		}
	}

	return nil
}

func (this *AvatarRoomPmtModel) Create() error {
	err := this.CreatePmt(2)
	if err != nil {
		return err
	}
	logger.PhoenixLoggerIns().Debug("create AvatarAllRoomModel success")
	return nil
}

func (this *AvatarRoomPmtModel) CreatePmt(roomId int32) error {
	configName := "t_RoomPmtTypeConfig_" + gconv.String(roomId)
	configs := logicConfigManager.PhoenixManagerLogicConfig().GetConfig(configName)
	if configs == nil {
		return gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_CONFIG_NOT_EXIST, "", nil),
			"config %s not exist", configName)
	}
	base, err := databaseManager.PhoenixManagerDatabase().DefaultDataBase()
	if err != nil {
		return err
	}
	for _, config := range configs.GetConfigs().Values() {
		record := config.(*gameConfig.RoomPmtType)
		if record.IsDefaultList == 1 {
			data := NewAvatarRoomOmtModelData()
			data.AvatarId = this.EntityDBID
			data.RoomId = roomId
			data.PmtId = record.Id
			data.Level = 1
			data.PmtLevel = 0
			data.Name = record.PmtTypeName
			data.UpdateTime = utils.GetNowTimeFormat()
			dataDBid, err := base.GetDb().Model(this.Template).Data(data).OmitEmpty().InsertAndGetId()
			if err != nil {
				logger.PhoenixLoggerIns().Errorf("create AvatarRoomPmtModel error %s", err.Error())
			}
			data.Dbid = dataType.PhoenixTypeDataBaseId(dataDBid)
			this.SetModelData(data.Dbid, data)
		}
	}
	return nil
}

func NewAvatarRoomPmtModel(name string, entityDBID dataType.PhoenixTypeDataBaseId) interfaces.IEntityModuleModel {
	m := &AvatarRoomPmtModel{
		ModuleModel: entityModel.NewModuleModel(name, entityDBID),
	}
	m.SetTemplate(NewAvatarRoomOmtModelData())
	return m
}
